package hx.design.chance;

/**
 * @author hx<titanrain@163.com>
 */
public class Chance {

    private final double ratio;
    public static final int GUARANTEED_VALUE = 1;

    public Chance(double ratio) {
        this.ratio = ratio;
    }

    public Chance and(Chance other) {
        return new Chance(this.ratio * other.ratio);
    }

    public Chance not() {
        return new Chance(GUARANTEED_VALUE - ratio);
    }

    public Chance or(Chance other) {
        return new Chance(this.ratio + other.ratio - and(other).ratio);
    }

    public Chance orOfDeMorganLaw(Chance other) {
        //DeMorgan's law: P(A||B) = !(!P(A) && !P(B))
        return (this.not().and(other.not())).not();
    }

    public static void main(String[] args) {
        System.out.println(new Chance(0.4));
    }

    @Override
    public boolean equals(Object other) {
        if (this == other) return true;
        if (!(other instanceof Chance)) return false;

        Chance chance = (Chance) other;

        if (Double.compare(chance.ratio, ratio) != 0) return false;

        return true;
    }

    @Override
    public int hashCode() {
        long temp = ratio != +0.0d ? Double.doubleToLongBits(ratio) : 0L;
        return (int) (temp ^ (temp >>> 32));
    }
}
